package com.ruoyi.customer.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.TMesCategory;
import com.ruoyi.customer.service.ITMesCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
/**
 * 往来客户分类Controller
 *
 * @author ruoyi
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/customer/category")
@Api(tags = "往来客户分类")
public class TMesCategoryController extends BaseController {
    @Autowired
    private ITMesCategoryService tMesCategoryService;

    /**
     * 查询往来客户分类列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody TMesCategory tMesCategory) {
        startPage();
        List<TMesCategory> list = tMesCategoryService.selectTMesCategoryList(tMesCategory);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody TMesCategory tMesCategory) {
        startOrderBy();
        List<TMesCategory> list = tMesCategoryService.selectTMesCategoryList(tMesCategory);
        return success(list);
    }

    /**
     * 导出往来客户分类列表
     */
    @Log(title = "往来客户分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, TMesCategory tMesCategory) {
        List<TMesCategory> list = tMesCategoryService.selectTMesCategoryList(tMesCategory);
        ExcelUtil<TMesCategory> util = new ExcelUtil<TMesCategory>(TMesCategory.class);
        util.exportExcel(response, list, "往来客户分类数据");
    }

    /**
     * 获取往来客户分类详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(tMesCategoryService.selectTMesCategoryById(id));
    }

    /**
     * 新增往来客户分类
     */
    @Log(title = "往来客户分类", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody TMesCategory tMesCategory) {
        if(tMesCategory.getId() == null) {
            tMesCategoryService.insertTMesCategory(tMesCategory);
        } else {
            tMesCategoryService.updateTMesCategory(tMesCategory);
        }
        return success(tMesCategory);
    }

    /**
     * 修改往来客户分类
     */
    @Log(title = "往来客户分类", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody TMesCategory tMesCategory) {
        return success(tMesCategoryService.updateTMesCategory(tMesCategory));
    }

    /**
     * 删除往来客户分类
     */
    @Log(title = "往来客户分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMesCategoryService.deleteTMesCategoryByIds(ids));
    }

    @Log(title = "往来客户分类", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody TMesCategory tMesCategory) {
        int num = tMesCategoryService.delete(tMesCategory);

        if(num == -1) {
           return ResponseModel.error("该节点下有子节点，不能直接删除!");
        }

        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<TMesCategory>> queryPage(@RequestBody TMesCategory tMesCategory) {
        Pages<TMesCategory> page = tMesCategoryService.queryPage(tMesCategory);
        return ResponseModel.of(page);
    }

    @PostMapping("/batchUpdate")
    @ApiOperation("批量更新")
    public AjaxResult updateTrees(@RequestBody List<TMesCategory> list) {
        Date now = new Date();
        Long uid = getUserId();
        String uname = getNickName();

        for(TMesCategory item : list) {
            item.setUpdateBy(uname);
            item.setUpdateId(uid);
            item.setUpdateTime(now);
            tMesCategoryService.updateTMesCategory(item);
        }
        return success();
    }
}
